<template>
  <el-form size="medium"
           label-position="right"
           label-width="200px">
    <el-form-item label="金融产品公司全称"
                  required>
      <el-col :span="7">
        <el-form-item v-if="!isInfo">
          <el-input v-model="supplier.companyName"
                    placeholder="请输入金融产品公司全称"
                    size="medium"></el-input>
        </el-form-item>
        <el-form-item v-else>
          <span>{{ supplier.companyName }}</span>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="金融产品经理"
                  required>
      <el-col :span="7">
        <el-form-item v-if="!isInfo">
          <el-input v-model="supplier.productManager"
                    placeholder="请输入金融产品经理"
                    size="medium"></el-input>
        </el-form-item>
        <el-form-item v-else>
          <span>{{ supplier.productManager }}</span>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="手机号"
                  required>
      <el-col :span="7">
        <el-form-item v-if="!isInfo">
          <el-input v-model.number="supplier.phone"
                    placeholder="请输入手机号"
                    size="medium"></el-input>
        </el-form-item>
        <el-form-item v-else>
          <span>{{ supplier.phone }}</span>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="金融产品特性标签"
                  v-if="supplier.label">
      <el-col :span="7">
        <el-form-item v-if="!isInfo">
          <el-input v-model="supplier.label"
                    placeholder="请以;隔开"
                    size="medium"></el-input>
        </el-form-item>
        <el-form-item v-else>
          <span>{{ supplier.label }}</span>
        </el-form-item>
      </el-col>
      <el-col :span="7">
        <span style="color: #b4b9c6;">（例如：申请速度快、下款额度高、黑户可申请）</span>
      </el-col>
    </el-form-item>
    <el-form-item v-if="!isInfo">
      <el-button type="primary"
                 @click="saveSupplier">发布</el-button>
      <el-button @click="() => {this.$router.go(-1)}">取消</el-button>
    </el-form-item>
    <el-form-item v-else>
      <el-button type="primary"
                 @click="() => {this.$router.go(-1)}">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<style>
.upload div input {
  display: none;
}
</style>

<script>
import '@/style/common.scss'
import { api } from '@/api'
import { getToken } from '@/utils/auth'
import { HTTPSUCCESS, HTTPDATAFAIL, HTTPPARAMFAIL, HTTPUNKOWNFAIL } from '@/components/constant'
import { devBaseURL } from '@/utils/http'

export default {
  data() {
    return {
      isInfo: this.$route.params.infoFlag === 1,
      supplier: {
        id: this.$route.params.supplierId || null,
        companyName: '',
        productManager: '',
        phone: '',
        label: ''
      }
    }
  },
  created() {
    this.getSupplier()
  },
  methods: {
    getToken,
    getSupplier() {
      if (this.supplier.id) {
        this.$http._get(api.SYS_SUPPLIER, this.supplier.id).then(
          res => {
            if (res.code === HTTPSUCCESS) {
              this.supplier = res.data
            } else {
              this.$msg._message(res.msg, 'error')
            }
          }
        )
      }
    },
    saveSupplier() {
      if (!this.supplier.companyName || !this.supplier.productManager || !this.supplier.phone) {
        return this.$msg._message('*为必填项', 'warning')
      } else {
        const params = {
          id: this.supplier.id,
          companyName: this.supplier.companyName,
          productManager: this.supplier.productManager,
          phone: this.supplier.phone,
          label: this.supplier.label || null,
          status: 0
        }
        this.$http.post(api.SYS_SUPPLIER_NEW, params).then(
          res => {
            if (res.code === HTTPSUCCESS) {
              this.$msg._message('添加成功！', 'success')
              this.$router.push({ name: 'supplier' })
            } else {
              this.$msg._message(res.msg, 'error')
            }
          }
        )
      }
    }
  }
}
</script>
